package com.tanhua.server.serivce;

import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.ManageApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.User;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.FreezeUser;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserStatusService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private ManageApi manageApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public Boolean findFreeze(User user) {
        if (user != null) {
            Long userId = user.getId();
            String value = redisTemplate.opsForValue().get(Constants.USER_FREEZE + userId);
            if (StringUtils.isNotEmpty(value) && !"".equals(value)) {
                FreezeUser freezeUser = manageApi.findByUserId(userId);
                if (freezeUser != null){
                    //获取冻结开始的时间
                    Long freezeStart = freezeUser.getFreezeStart();
                    //获取冻结范围
                    Integer freezingRange = freezeUser.getFreezingRange();
                    //获取冻结时长
                    Integer freezingTime = freezeUser.getFreezingTime();
                    //如果冻结范围为1，限制用户登录
                    if (freezingRange == 1){
                        //当前时间
                        Long nowTime = System.currentTimeMillis();
                        //冻结开始+冻结时长
                        Long time = Long.parseLong(freezingTime.toString());
                        long freezeTime = freezeStart + (time * 24 * 60 * 60 * 1000);
                        //现在时间<冻结开始+冻结时长,未解冻,抛出异常
                        if (nowTime < freezeTime){
                            return true;
                        } else {
                            manageApi.deleteByUserId(userId);
                            UserInfo userInfo = userInfoApi.findById(userId);
                            userInfo.setUserStatus("1");
                            userInfoApi.update(userInfo);
                            System.out.println("已经自我解冻");
                        }
                    }
                }
            }
            //修改userInfo的状态
            UserInfo userInfo = userInfoApi.findById(userId);
            userInfo.setUserStatus("1");
            userInfoApi.update(userInfo);
            System.out.println("已经自我解冻");
        }
        return false;
    }

    public Boolean findCommentFreeze(User user) {
        if (user != null) {
            Long userId = user.getId();
            String value = redisTemplate.opsForValue().get(Constants.USER_FREEZE + userId);
            if (StringUtils.isNotEmpty(value) && !"".equals(value)) {
                FreezeUser freezeUser = manageApi.findByUserId(userId);
                if (freezeUser != null){
                    //获取冻结开始的时间
                    Long freezeStart = freezeUser.getFreezeStart();
                    //获取冻结范围
                    Integer freezingRange = freezeUser.getFreezingRange();
                    //获取冻结时长
                    Integer freezingTime = freezeUser.getFreezingTime();
                    //如果冻结范围为2，限制用户评论
                    if (freezingRange == 2){
                        //当前时间
                        Long nowTime = System.currentTimeMillis();
                        //冻结开始+冻结时长
                        Long time = Long.parseLong(freezingTime.toString());
                        long freezeTime = freezeStart + (time * 24 * 60 * 60 * 1000);
                        //现在时间<冻结开始+冻结时长,未解冻,抛出异常
                        if (nowTime < freezeTime){
                            return true;
                        } else {
                            manageApi.deleteByUserId(userId);
                            UserInfo userInfo = userInfoApi.findById(userId);
                            userInfo.setUserStatus("1");
                            userInfoApi.update(userInfo);
                            System.out.println("已经自我解冻");
                        }
                    }
                }
            }
            //修改userInfo的状态
            UserInfo userInfo = userInfoApi.findById(userId);
            userInfo.setUserStatus("1");
            userInfoApi.update(userInfo);
            System.out.println("已经自我解冻");
        }
        return false;
    }

    public Boolean findMovementFreeze(User user) {
        if (user != null) {
            Long userId = user.getId();
            String value = redisTemplate.opsForValue().get(Constants.USER_FREEZE + userId);
            if (StringUtils.isNotEmpty(value) && !"".equals(value)) {
                FreezeUser freezeUser = manageApi.findByUserId(userId);
                if (freezeUser != null){
                    //获取冻结开始的时间
                    Long freezeStart = freezeUser.getFreezeStart();
                    //获取冻结范围
                    Integer freezingRange = freezeUser.getFreezingRange();
                    //获取冻结时长
                    Integer freezingTime = freezeUser.getFreezingTime();
                    //如果冻结范围为3，限制用户发布动态
                    if (freezingRange == 3){
                        //当前时间
                        Long nowTime = System.currentTimeMillis();
                        //冻结开始+冻结时长
                        Long time = Long.parseLong(freezingTime.toString());
                        long freezeTime = freezeStart + (time * 24 * 60 * 60 * 1000);
                        //现在时间<冻结开始+冻结时长,未解冻,抛出异常
                        if (nowTime < freezeTime){
                            return true;
                        } else {
                            //在mongoDB中删除freeze_user的数据
                            manageApi.deleteByUserId(userId);

                        }
                    }
                }
            }
            //修改userInfo的状态
            UserInfo userInfo = userInfoApi.findById(userId);
            userInfo.setUserStatus("1");
            userInfoApi.update(userInfo);
            System.out.println("已经自我解冻");
        }
        return false;
    }
}
